#include "ReadonlyErr.h"

#include <stdarg.h>
#include <string.h>
#include <fan-llvm/core/Core.h>
#include "Str.h"
#include "Type.h"


Type* ReadonlyErr_classType = NULL;
VTable* ReadonlyErr_classVTable = NULL;


/**********************************************
 *
 * Methods and fields
 *
 **********************************************/


/* Void make(Str msg, Err? cause) */
void ReadonlyErr_make_0(ReadonlyErr* self)
{
  ReadonlyErr_make(self, Str_defVal(), NULL);
}


/* Void make(Str msg, Err? cause) */
void ReadonlyErr_make_1(ReadonlyErr* self, Str* msg)
{
  ReadonlyErr_make(self, msg, NULL);
}


/* Void make(Str msg, Err? cause) */
void ReadonlyErr_make(ReadonlyErr* self, Str* msg, Err* cause)
{
  Err_make((Err*)self, msg, cause);
}


/**********************************************
 *
 * Extra methods
 *
 **********************************************/


/* Create a new ReadonlyErr instance and initialize it with ReadonlyErr_make() */
ReadonlyErr* ReadonlyErr_new(Str* msg, Err* cause)
{
  ReadonlyErr* err = (ReadonlyErr*)Obj_allocate(sizeof(ReadonlyErr), ReadonlyErr_classVTable);
  ReadonlyErr_make(err, msg, cause);
  return err;
}


/* Create a new ReadonlyErr instance and initialize it with the message given by the specified format.
 * 'msgFormat' uses the same format as Str_newWithFormat() */
ReadonlyErr* ReadonlyErr_newWithFormattedMsg(const char* msgFormat, ...)
{
  va_list args;
  va_start(args, msgFormat);
  Str* msg = Str_newWithFormatValist(msgFormat, args);
  va_end(args);
  return ReadonlyErr_new(msg, NULL);
}


/**********************************************
 *
 * Type-system functions
 *
 **********************************************/


Type* ReadonlyErr_initType(void)
{
  if (ReadonlyErr_classType == NULL)
  {
    Type* baseType = Err_initType();
    VTable* vtable = ReadonlyErr_initVTable();
    Type* type = Type_new("ReadonlyErr", baseType, vtable, sizeof(ReadonlyErr), sizeof(ReadonlyErrVTable));
    Type_registerCtorImplementation(type, "make", ReadonlyErr_make_0, 0);
    Type_registerCtorImplementation(type, "make", ReadonlyErr_make_1, 1);
    Type_registerCtorImplementation(type, "make", ReadonlyErr_make, 2);
    ReadonlyErr_classType = type;
    ReadonlyErr_initStatic();
  }
  return ReadonlyErr_classType;
}


VTable* ReadonlyErr_initVTable(void)
{
  if (ReadonlyErr_classVTable == NULL)
  {
    VTable* vtable = memoryAlloc(sizeof(ReadonlyErrVTable));
    memcpy(vtable, Err_initVTable(), sizeof(ErrVTable));
    ReadonlyErr_classVTable = vtable;
  }
  return ReadonlyErr_classVTable;
}


void ReadonlyErr_initStatic(void)
{
}
